package com.whjk.safework.service.impl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.query.Conditional;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row;
import com.whjk.safework.service.IWorkerService;
import com.whjk.safework.utils.LoaclDateUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkerService implements IWorkerService {



    /*
     获取周度周期的任务
     */
    @Override
    public List<Row> getWorkerWeekRecord(Integer workerId, String month) {
        String sql = "SELECT t_worker_target.id,t_worker_target.safetarget_id,t_worker_target.target_id,t_org_safetarget.check_pc,t_worker_target.kind_id," +
                "t_org_target.weight AS aweight,t_org_target.kind_id as kid,t_org_safetarget.weight AS bweight,pass_time,my_check_time,t_worker_target.check_result,my_check_result,start_time,end_time,day " +
                "from  t_worker_target join t_org_safetarget ON t_worker_target.safetarget_id = t_org_safetarget.id join t_org_target ON t_worker_target.target_id = t_org_target.id " +
                " where worker_id = ? and t_worker_target.check_pc='1次/每周' and   DATE_FORMAT(start_time,'%Y-%m')= ?";
        List<Row>   rows    =  Db.selectListBySql(sql,workerId,month);
        return rows;
    }


    /*
     获取月度周期的任务
     */
    @Override
    public List<Row> getWorkerMonthRecord(Integer workerId, String month) {
        String sql = "SELECT t_worker_target.id,t_worker_target.safetarget_id,t_worker_target.target_id,t_org_safetarget.check_pc,t_worker_target.kind_id," +
                " t_org_target.weight AS aweight,t_org_target.kind_id as kid,t_org_safetarget.weight AS bweight,pass_time,my_check_time,t_worker_target.check_result,my_check_result,start_time,end_time,day " +
                " from  t_worker_target join t_org_safetarget ON t_worker_target.safetarget_id = t_org_safetarget.id join t_org_target ON t_worker_target.target_id = t_org_target.id " +
                " where worker_id = ? and t_worker_target.check_pc='1次/每月' and   DATE_FORMAT(start_time,'%Y-%m')= ?";
        List<Row>   rows    =  Db.selectListBySql(sql,workerId,month);
        return  rows;
    }

    /*
     获取季度周期的任务
     */
    @Override
    public List<Row> getWorkerQuarterRecord(Integer workerId, String month) {
        Row     row   =     LoaclDateUtil.getThisQuarterByCurrentMonth(month);
        String  sql   =     "select t_worker_target.id,t_worker_target.safetarget_id,t_worker_target.target_id,t_org_safetarget.check_pc,t_worker_target.kind_id," +
                " t_org_target.weight AS aweight,t_org_target.kind_id as kid,t_org_safetarget.weight AS bweight,pass_time,my_check_time,t_worker_target.check_result,my_check_result,start_time,end_time,day " +
                " from  t_worker_target join t_org_safetarget ON t_worker_target.safetarget_id = t_org_safetarget.id join t_org_target ON t_worker_target.target_id = t_org_target.id " +
                " where worker_id = ? and t_worker_target.check_pc='1次/每季度' and start_time<=? and end_time>=?";
        List<Row>   rows    =  Db.selectListBySql(sql,workerId,row.get("startDay"),row.get("endDay"));
        return  rows;
    }


    /*
    获取年周期的任务
    */
    @Override
    public List<Row> getWorkerYearRecord(Integer workerId, String year) {
        String sql = "SELECT t_worker_target.id,t_worker_target.safetarget_id,t_worker_target.target_id,t_org_safetarget.check_pc,t_worker_target.kind_id," +
                " t_org_target.weight AS aweight,t_org_target.kind_id as kid,t_org_safetarget.weight AS bweight,pass_time,my_check_time,t_worker_target.check_result,my_check_result,start_time,end_time,day " +
                " from  t_worker_target join t_org_safetarget ON t_worker_target.safetarget_id = t_org_safetarget.id join t_org_target ON t_worker_target.target_id = t_org_target.id " +
                " where worker_id = ? and t_worker_target.check_pc='1次/每年' and   DATE_FORMAT(start_time,'%Y')= ?";

        List<Row>   rows    =  Db.selectListBySql(sql,workerId,year);
        return  rows;
    }

    @Override
    public List<Row> getWorkerAllYearRecord(Integer workerId, String year) {
        String sql = "SELECT t_worker_target.id,t_worker_target.safetarget_id,t_worker_target.target_id,t_org_safetarget.check_pc,t_worker_target.kind_id," +
                " t_org_target.weight AS aweight,t_org_target.kind_id as kid,t_org_safetarget.weight AS bweight,pass_time,my_check_time,t_worker_target.check_result,my_check_result,start_time,end_time,day " +
                " from  t_worker_target join t_org_safetarget ON t_worker_target.safetarget_id = t_org_safetarget.id join t_org_target ON t_worker_target.target_id = t_org_target.id " +
                " where worker_id = ?  and   DATE_FORMAT(start_time,'%Y')= ?";

        List<Row>   rows    =  Db.selectListBySql(sql,workerId,year);
        return  rows;
    }

    @Override
    public List<Row> getPushWorker() {
        String      sql     = "SELECT t_worker.id,t_worker.org_id,t_worker.tel,t_user.open_id from  t_worker join t_user ON t_worker.user_id = t_user.id  where  t_user.is_bind_userinfo=1 ";
        List<Row>   rows    =  Db.selectListBySql(sql);

        return  rows;
    }





    @Override
    public int addPushRecord(Row row) {
       return   Db.insert("t_push_record",row);
    }


    public int addUpdateWorkYearRecord(Integer workerId, Integer year, BigDecimal myscore, BigDecimal leaderScore,BigDecimal averageScore){

        Row row    =  new Row();
        Row row_worker     = Db.selectOneBySql("select * from t_worker where id=?",workerId);

        row.set("worker_id",workerId);
        row.set("year",year);
        row.set("my_score",myscore);
        row.set("leader_score",leaderScore);
        row.set("average_score",averageScore);
        row.set("last_time", DateUtil.date());
        row.set("org_id",row_worker.get("org_id",0));
        row.set("position_id",row_worker.get("position_id",0));


        if(Db.selectCount("select count(*) from t_worker_year_score where worker_id=? and year=?",workerId,year)>0){
            Row rowOne    =  new Row();
                rowOne.set("worker_id",workerId);
                rowOne.set("year",year);
                rowOne     = Db.selectOneBySql("select * from t_worker_year_score where worker_id=? and year=?",workerId,year);
            row.set("id",rowOne.getInt("id"));
            row.set("last_time", DateUtil.date());
            Map<String,Object>  map = new HashMap<>();
            map.put("id",rowOne.getInt("id"));
            return  Db.updateByMap("t_worker_year_score",row,map);
        }
        else if(Db.selectCount("select count(*) from t_worker_year_score where worker_id=? and year=?",workerId,year)==0){
            return  Db.insert("t_worker_year_score",row);
        }
        else return  0;
    }


    public int addUpdateWorkMonthRecord(Integer workerId, String  month, BigDecimal myscore, BigDecimal leaderScore,BigDecimal averageScore){

        Row row             =  new Row();
        Row row_worker      = Db.selectOneBySql("select * from t_worker where id=?",workerId);

        row.set("worker_id",workerId);
        row.set("year",month.substring(0,4));
        row.set("month",month);
        row.set("my_score",myscore);
        row.set("leader_score",leaderScore);
        row.set("average_score",averageScore);
        row.set("last_time", DateUtil.date());
        row.set("org_id",row_worker.get("org_id",0));
        row.set("position_id",row_worker.get("position_id",0));

        if(Db.selectCount("select count(*) from t_worker_month_score where worker_id=? and month=?",workerId,month)>0){
            Row rowOne    =  new Row();
                rowOne.set("worker_id",workerId);
                rowOne.set("year",month.substring(0,4));
                rowOne.set("month",month);
                rowOne     = Db.selectOneBySql("select * from t_worker_month_score where worker_id=? and month=?",workerId,month);
            row.set("id",rowOne.getInt("id"));
            row.set("last_time", DateUtil.date());
            Map<String,Object>  map = new HashMap<>();
            map.put("id",rowOne.getInt("id"));

            Row rowNum = this.getWorkRecordNum(workerId,month);
                row.set("undo_num", rowNum.get("undo_num",0));
                row.set("ok_num", rowNum.get("done_num",0));
                row.set("total_num", rowNum.get("num",0));

            return  Db.updateByMap("t_worker_month_score",row,map);
        }
        else if(Db.selectCount("select count(*) from t_worker_month_score where worker_id=? and month=?",workerId,month)==0){

            Row rowNum = this.getWorkRecordNum(workerId,month);
            row.set("undo_num", rowNum.get("undo_num",0));
            row.set("ok_num", rowNum.get("done_num",0));
            row.set("total_num", rowNum.get("num",0));

            return  Db.insert("t_worker_month_score",row);
        }
        else return  0;
    }


    public void refreshWorkerScore(Integer workerId, Integer year){

        String      orgTargetSql     = "select DISTINCT(t_worker_target.target_id) as targetId,target_str,kind_id,t_dic.name as kName from t_worker_target join t_dic on t_worker_target.kind_id=t_dic.id  where worker_id = ? and year=?";
        List<Row>   orgTargetRows    = Db.selectListBySql(orgTargetSql, workerId, year);

        //  System.out.println(orgTargetRows);
        for (Row row_1 : orgTargetRows) {
            String      orgSafeTargetSql    = "select DISTINCT(t_worker_target.safetarget_id) as safetargetId from t_worker_target  where worker_id = ? and year=? and target_id=?";
            List<Row>   orgSafeTargetRows   =  Db.selectListBySql(orgSafeTargetSql, workerId, year, row_1.getInt("targetId"));
            row_1.set("children", orgSafeTargetRows);
        }
        List<Row>  allList     =   this.getWorkerAllYearRecord(workerId,year+"");

        for (Row row_1_new:orgTargetRows) {
            List<Row>  row_2_list    =  (List<Row>)row_1_new.get("children");
            for (Row row_2_new:row_2_list) {
                List<Row> children  = new ArrayList<>();
                for(Row r:allList) {
                    int row2_safetargetId  =    row_2_new.getInt("safetargetId");
                    int r_safetargetId     =    r.getInt("safetarget_id");

                    if (row2_safetargetId == r_safetargetId){
                        children.add(r);
                    }
                }
                row_2_new.set("children",children);
            }
            row_1_new.set("children",row_2_list);
        }

        BigDecimal  E_leader            = new BigDecimal(1);
        BigDecimal  E_my                = new BigDecimal(1);
        BigDecimal  result_leader_score = new BigDecimal(BigInteger.ZERO);
        BigDecimal  result_my_score     = new BigDecimal(BigInteger.ZERO);

        for(Row jsr:orgTargetRows){

            List<Row> safeRowList     = (List<Row>)jsr.get("children");
            BigDecimal leader_score_1 = new BigDecimal(BigInteger.ZERO);
            BigDecimal my_score_1     = new BigDecimal(BigInteger.ZERO);
            int orgTargetNum          = 0;
            for (Row saferow:safeRowList) {
                List<Row> scoreRowList = (List<Row>)saferow.get("children");
                int         num             = 0;
                BigDecimal  leader_score    = new BigDecimal(BigInteger.ZERO);
                BigDecimal  my_score        = new BigDecimal(BigInteger.ZERO);

                Integer[]  ids = new Integer[scoreRowList.size()];

                for (Row scorerow:scoreRowList) {
                    ids[num] = scorerow.getInt("id");
                    BigDecimal aWeight      =   scorerow.getBigDecimal("aweight");
                    BigDecimal bWeight      =   scorerow.getBigDecimal("bweight");
                    BigDecimal a            =   new BigDecimal(10);
                    BigDecimal b            =   new BigDecimal(10);

                    if(scorerow.getInt("kind_id")==2){
                        LocalDateTime endTime   =    scorerow.getLocalDateTime("end_time");
                        // 结束时间在当前时间后  未逾期过程
                        if(endTime.isAfter(LocalDateTime.now())){
                           /* if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                a            =  new BigDecimal(10) ;
                            }
                            else   a         =  scorerow.getBigDecimal("check_result");
                            if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                b            =  new BigDecimal(10) ;
                            }
                            else   b         =  scorerow.getBigDecimal("my_check_result");
                            */
                            if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                b       =  new BigDecimal(0) ;
                            }
                            else   b         =  scorerow.getBigDecimal("my_check_result");

                            if(b.compareTo(BigDecimal.ZERO) > 0){ //自评了
                                if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                    a            =  new BigDecimal(10) ;
                                }
                                else   a         =  scorerow.getBigDecimal("check_result");
                            }
                            else {
                                if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                    a            =  new BigDecimal(0) ;
                                }
                                else   a         =  scorerow.getBigDecimal("check_result");
                            }
                        }
                        else { // 结束时间在当前时间前面  已经逾期过程
                            if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                a            =  new BigDecimal("0") ;
                            }
                            else   a         =  scorerow.getBigDecimal("check_result");
                            if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                b            =  new BigDecimal("0") ;
                            }
                            else   b         =  scorerow.getBigDecimal("my_check_result");
                        }
                        BigDecimal leader_temp_score   =  aWeight .multiply(bWeight).multiply(a).multiply(new BigDecimal(10));
                        BigDecimal my_temp_score       =  aWeight .multiply(bWeight).multiply(b).multiply(new BigDecimal(10));
                        leader_score        =  leader_score.add(leader_temp_score);
                        my_score            =  my_score.add(my_temp_score);
                    }
                    if(scorerow.getInt("kind_id")==1&& a.compareTo(new BigDecimal(BigInteger.ZERO))==0){
                        E_leader=  new BigDecimal(BigInteger.ZERO);
                    }
                    if(scorerow.getInt("kind_id")==1&& b.compareTo(new BigDecimal(BigInteger.ZERO))==0){
                        E_my=  new BigDecimal(BigInteger.ZERO);
                    }
                    num++;
                }
                if(num>=1) {
                    leader_score    = leader_score.divide(new BigDecimal(num),2);
                    my_score        = my_score.divide(new BigDecimal(num),2);
                }
                saferow.set("leader_score",leader_score);
                saferow.set("my_score",my_score);
                saferow.set("childrenIds",ids);
                leader_score_1 =  leader_score_1.add(leader_score);
                my_score_1     =  my_score_1.add(my_score);
                orgTargetNum++;
            }
            if(orgTargetNum>=1){
                leader_score_1 =  leader_score_1.divide(new BigDecimal(orgTargetNum),2);
                my_score_1     =  my_score_1.divide(new BigDecimal(orgTargetNum),2);
            }
            jsr.set("leader_score",leader_score_1);
            jsr.set("my_score",my_score_1);
            result_leader_score     =    result_leader_score.add(leader_score_1);
            result_my_score         =    result_my_score.add(my_score_1);
        }
        result_leader_score         =    result_leader_score.multiply(E_leader) ;
        result_my_score             =    result_my_score.multiply(E_my) ;

        BigDecimal temp_leader_score = result_leader_score.multiply(new BigDecimal("0.4"));
        BigDecimal temp_my_score     = result_my_score.multiply(new BigDecimal("0.6"));

        Row     result = new Row();
        result.set("leadScore",result_leader_score);
        result.set("myScore",result_my_score);
        result.set("averageScore", temp_leader_score.add(temp_my_score)); //平均分
        result.set("E",E_leader);
        this.addUpdateWorkYearRecord(workerId,year,result_leader_score,result_my_score,temp_leader_score.add(temp_my_score));
    }

    public void refreshWorkersMonthSocre(Integer workerId,String month){

        String        orgTargetSql       =  "select DISTINCT(t_worker_target.target_id) as targetId,target_str,kind_id,t_dic.name as kName from t_worker_target join t_dic on t_worker_target.kind_id=t_dic.id  where worker_id = ? and DATE_FORMAT(start_time,'%Y')=?";
        List<Row>     orgTargetRows      =  Db.selectListBySql(orgTargetSql,workerId,month.substring(0,4));
        for (Row row_1:orgTargetRows) {
            String                  orgSafeTargetSql   =  "select DISTINCT(t_worker_target.safetarget_id) as safetargetId,target_str,dutyinfo_str,plan_str,check_pc,check_str from t_worker_target  where worker_id = ? and DATE_FORMAT(start_time,'%Y')=? and target_id=?";
            List<Row>               orgSafeTargetRows  =   Db.selectListBySql(orgSafeTargetSql,workerId,month.substring(0,4),row_1.getInt("targetId"));
            row_1.set("children",orgSafeTargetRows);
        }
        List<Row>  allList          =   new ArrayList<>(); // 员工任务记录
        List<Row>  rowList_week     =   this.getWorkerWeekRecord(workerId,month); // 获取 周任务记录
        List<Row>  rowList_month    =   this.getWorkerMonthRecord(workerId,month);// 获取 月任务记录
        List<Row>  rowList_quarter  =   this.getWorkerQuarterRecord(workerId,month);// 获取 季度任务记录
        List<Row>  rowList_year     =   this.getWorkerYearRecord(workerId,month.substring(0,4));// 获取 年任务记录

        allList.addAll(rowList_week);
        allList.addAll(rowList_month);
        allList.addAll(rowList_quarter);
        allList.addAll(rowList_year);
        for (Row row_1_new:orgTargetRows) {
            List<Row>  row_2_list    =  (List<Row>)row_1_new.get("children");
            for (Row row_2_new:row_2_list) {
                List<Row> children  = new ArrayList<>();
                for(Row r:allList) {
                    int row2_safetargetId  =    row_2_new.getInt("safetargetId");
                    int r_safetargetId     =    r.getInt("safetarget_id");
                    if (row2_safetargetId == r_safetargetId){
                        children.add(r);
                    }
                }
                row_2_new.set("children",children);
            }
            row_1_new.set("children",row_2_list);
        }

        BigDecimal  E_leader            = new BigDecimal(1);
        BigDecimal  E_my                = new BigDecimal(1);
        BigDecimal  result_leader_score = new BigDecimal(BigInteger.ZERO);
        BigDecimal  result_my_score     = new BigDecimal(BigInteger.ZERO);

        for(Row jsr:orgTargetRows){  // 目标指标
            List<Row> safeRowList     = (List<Row>)jsr.get("children");
            BigDecimal leader_score_1 = new BigDecimal(BigInteger.ZERO);
            BigDecimal my_score_1     = new BigDecimal(BigInteger.ZERO);
            int orgTargetNum          = 0;
            for (Row saferow:safeRowList) { // 目标履职条款指标

                List<Row> scoreRowList = (List<Row>)saferow.get("children");
                int         num             = 0;
                BigDecimal  leader_score    = new BigDecimal(BigInteger.ZERO);
                BigDecimal  my_score        = new BigDecimal(BigInteger.ZERO);

                Integer[]  ids = new Integer[scoreRowList.size()];

                for (Row scorerow:scoreRowList) {
                    ids[num] = scorerow.getInt("id");
                    BigDecimal aWeight      =   scorerow.getBigDecimal("aweight");
                    BigDecimal bWeight      =   scorerow.getBigDecimal("bweight");
                    BigDecimal a            =   new BigDecimal(10); // 领导评分
                    BigDecimal b            =   new BigDecimal(10);//  自评分

                    if(scorerow.getInt("kind_id")==2){
                        LocalDateTime endTime   =    scorerow.getLocalDateTime("end_time");
                        // 结束时间在当前时间后  未逾期过程
                        if(endTime.isAfter(LocalDateTime.now())){

                            if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                b       =  new BigDecimal(0) ;
                            }
                            else   b         =  scorerow.getBigDecimal("my_check_result");

                            if(b.compareTo(BigDecimal.ZERO) > 0){ //自评了
                                if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                    a            =  new BigDecimal(10) ;
                                }
                                else   a         =  scorerow.getBigDecimal("check_result");
                            }
                            else {
                                if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                    a            =  new BigDecimal(0) ;
                                }
                                else   a         =  scorerow.getBigDecimal("check_result");
                            }

                        }
                        else { // 结束时间在当前时间前面  已经逾期过程
                            if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                a            =  new BigDecimal("0") ;
                            }
                            else   a         =  scorerow.getBigDecimal("check_result");
                            if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                b            =  new BigDecimal("0") ;
                            }
                            else   b         =  scorerow.getBigDecimal("my_check_result");
                        }
                        BigDecimal leader_temp_score   =  aWeight .multiply(bWeight).multiply(a).multiply(new BigDecimal(10));
                        BigDecimal my_temp_score       =  aWeight .multiply(bWeight).multiply(b).multiply(new BigDecimal(10));
                        leader_score        =  leader_score.add(leader_temp_score);
                        my_score            =  my_score.add(my_temp_score);
                    }
                    if(scorerow.getInt("kind_id")==1&& a.compareTo(new BigDecimal(BigInteger.ZERO))==0){
                        E_leader=  new BigDecimal(BigInteger.ZERO);
                    }
                    if(scorerow.getInt("kind_id")==1&& b.compareTo(new BigDecimal(BigInteger.ZERO))==0){
                        E_my=  new BigDecimal(BigInteger.ZERO);
                    }
                    num++;
                }
                if(num>=1) {
                    leader_score    = leader_score.divide(new BigDecimal(num),2);
                    my_score        = my_score.divide(new BigDecimal(num),2);
                }
                saferow.set("leader_score",leader_score);
                saferow.set("my_score",my_score);
                saferow.set("childrenIds",ids);
                leader_score_1 =  leader_score_1.add(leader_score);
                my_score_1     =  my_score_1.add(my_score);
                orgTargetNum++;
            }
            if(orgTargetNum>=1){
                leader_score_1 =  leader_score_1.divide(new BigDecimal(orgTargetNum),2);
                my_score_1     =  my_score_1.divide(new BigDecimal(orgTargetNum),2);
            }
            jsr.set("leader_score",leader_score_1);
            jsr.set("my_score",my_score_1);
            result_leader_score     =    result_leader_score.add(leader_score_1);
            result_my_score         =    result_my_score.add(my_score_1);
        }
        result_leader_score         =    result_leader_score.multiply(E_leader) ;
        result_my_score             =    result_my_score.multiply(E_my) ;

        BigDecimal temp_leader_score = result_leader_score.multiply(new BigDecimal("0.4"));
        BigDecimal temp_my_score     = result_my_score.multiply(new BigDecimal("0.6"));

        Row     result = new Row();
                result.set("leadScore",result_leader_score);
                result.set("myScore",result_my_score);
                result.set("averageScore", temp_leader_score.add(temp_my_score)); //平均分
                result.set("E",E_leader);
        this.addUpdateWorkMonthRecord(workerId,month,result_leader_score,result_my_score,temp_leader_score.add(temp_my_score));

    }



    public List<Row> getWorkerAllRecord(Integer workerId,String month) {

        String orgTargetSql = "select DISTINCT(t_worker_target.target_id) as targetId,target_str,kind_id,t_dic.name as kName from t_worker_target join t_dic on t_worker_target.kind_id=t_dic.id  where worker_id = ? and DATE_FORMAT(start_time,'%Y')=?";
        List<Row> orgTargetRows = Db.selectListBySql(orgTargetSql, workerId, month.substring(0, 4));
        for (Row row_1 : orgTargetRows) {
            String orgSafeTargetSql = "select DISTINCT(t_worker_target.safetarget_id) as safetargetId,target_str,dutyinfo_str,plan_str,check_pc,check_str from t_worker_target  where worker_id = ? and DATE_FORMAT(start_time,'%Y')=? and target_id=?";
            List<Row> orgSafeTargetRows = Db.selectListBySql(orgSafeTargetSql, workerId, month.substring(0, 4), row_1.getInt("targetId"));
            row_1.set("children", orgSafeTargetRows);
        }
        List<Row> allList = new ArrayList<>(); // 员工任务记录
        List<Row> rowList_week = this.getWorkerWeekRecord(workerId, month); // 获取 周任务记录
        List<Row> rowList_month = this.getWorkerMonthRecord(workerId, month);// 获取 月任务记录
        List<Row> rowList_quarter = this.getWorkerQuarterRecord(workerId, month);// 获取 季度任务记录
        List<Row> rowList_year = this.getWorkerYearRecord(workerId, month.substring(0, 4));// 获取 年任务记录

        allList.addAll(rowList_week);
        allList.addAll(rowList_month);
        allList.addAll(rowList_quarter);
        allList.addAll(rowList_year);
        for (Row row_1_new : orgTargetRows) {
            List<Row> row_2_list = (List<Row>) row_1_new.get("children");
            for (Row row_2_new : row_2_list) {
                List<Row> children = new ArrayList<>();
                for (Row r : allList) {
                    int row2_safetargetId = row_2_new.getInt("safetargetId");
                    int r_safetargetId = r.getInt("safetarget_id");
                    if (row2_safetargetId == r_safetargetId) {
                        children.add(r);
                    }
                }
                row_2_new.set("children", children);
            }
            row_1_new.set("children", row_2_list);
        }
        return orgTargetRows;
    }


    public Row getWorkRecordNum(Integer workerId,String month){
        List<Row>   rows    = this.getWorkerAllrRecord(workerId, month);
        int     num          = 0;
        int     undo_num     = 0;
        int     done_num     = 0;

        for(Row row:rows){
            if(row.getInt("my_check_result")==0|| ObjectUtil.isEmpty(row.getInt("my_check_result"))){
                undo_num++;
            }else{
                done_num++;
            }
            num++;
        }
        Row     row    =  new Row();
        row.set("num",num);
        row.set("undo_num",undo_num);
        row.set("done_num",done_num);
        return row;
    }



    @Override
    public List<Row> getWorkerAllrRecord(Integer workerId, String month) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

        // 解析 month 字符串为 YearMonth 对象
        YearMonth yearMonth = YearMonth.parse(month, formatter);

        // 加一个月
        YearMonth nextMonth = yearMonth.plusMonths(1);
        String nextMonthStr =nextMonth.format(formatter);

        String  sql   =     "select t_worker_target.id,t_worker_target.safetarget_id,t_worker_target.target_id,t_org_safetarget.check_pc,t_worker_target.kind_id," +
                            " t_org_target.weight AS aweight,t_org_target.kind_id as kid,t_org_safetarget.weight AS bweight,pass_time,my_check_time,t_worker_target.check_result,my_check_result,start_time,end_time,day " +
                            " from  t_worker_target join t_org_safetarget ON t_worker_target.safetarget_id = t_org_safetarget.id join t_org_target ON t_worker_target.target_id = t_org_target.id " +
                            " where worker_id = ? and  t_worker_target.kind_id <> 1  and   DATE_FORMAT(end_time,'%Y-%m')=?";
        List<Row>   rows    =  Db.selectListBySql(sql,workerId,nextMonthStr);
        return  rows;
    }


}
